package com.sumainfor.wx.tenant.controller;


import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.wechat.WxUtil;
import com.sumainfor.wx.tenant.feign.OperationsFeignClient;
import com.sumainfor.wx.tenant.service.WechatService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 微信相关的coller 以及个人中心
 *
 * @author xiaoma
 */
@Controller
@RequestMapping("/wechat")
public class WechatColler {

    private Logger log = LoggerFactory.getLogger(WechatColler.class);

    @Autowired
    private WechatService wxbiz;

    @Autowired
    private OperationsFeignClient operationsFeignClient;

    @Autowired
    private WxUtil wxUtil;

    /**
     * 微信授权登录 判断opid是否绑定了
     *
     * @author xiaoma
     */
    @ResponseBody
    @RequestMapping(value = "/getWechatUser", method = RequestMethod.GET)
    public JsonResult getWechatUser(@RequestParam Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        Map<String, Object> isBind = wxbiz.isBind(params);
        return result.put(isBind);
    }


    /**
     * 微信分享签名密钥
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getQm")
    public JsonResult getQm(@RequestParam Map<String, Object> params) {
        log.info("请求的参数 ======> {}", params);
        //前端必传参数
        String url = MapUtils.getString(params, "url");
        if (StringUtils.isBlank(url)) {
            return JsonResult.errorForEmpty();
        }
        String accessToken = wxUtil.getAccessToken();
        if (StringUtils.isBlank(accessToken)) {
            return JsonResult.error("获取签名失败");
        }

        Map<String, Object> ticket = wxUtil.getTicket(url);
        return JsonResult.success(ticket);
    }

    /**
     * 用户通过手机验证码验证之后填写密码进行注册
     *
     * @param params
     * @return
     * @throws Exception
     * @author xiaoma
     */
    @ResponseBody
    @RequestMapping("/addusr")
    public JsonResult addusr(@RequestBody Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();

        if (params.get("phone") != null && params.get("uopid") != null && params != null) {

            result.put(wxbiz.addusr(params));

        } else {
            result.putFailed("请传入必传参数");
        }

        return result;
    }

    /**
     * 根据TenId和openid 查询个人信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/getUsr")
    public JsonResult getUsr(@RequestParam Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (!FieldCheckUtil.isMapFieldNotNull(params, new String[]{"TenId"})) {
            result.putFailed("TenId不能为空！");
            return result;
        }
        if (!FieldCheckUtil.isMapFieldNotNull(params, new String[]{"uopid"})) {
            result.putFailed("uopid不能为空！");
            return result;
        }
        result.put(wxbiz.getUsr(params));
        return result;
    }

    /**
     * 根据TenId修改用户的名字或者图片
     *
     * @param params
     * @return
     */

    @RequestMapping("/updateUsrNameorUimg")
    @ResponseBody
    public JsonResult updateUsrNameorUimg(@RequestParam Map<String, Object> params) {

        return wxbiz.updateUsrNameorUimg(params);

    }

    /**
     * 根据TenId修改用户的手机号码
     *
     * @param params
     * @return
     */

    @RequestMapping("/updateTenPhoneByTenId")
    @ResponseBody
    public JsonResult updateTenPhoneByTenId(@RequestParam Map<String, Object> params) {

        return wxbiz.updateTenPhoneByTenId(params);

    }


    /**
     * 租客成为业主
     *
     * @param params
     * @return
     */
    @RequestMapping("/AstheOwner")
    @ResponseBody
    public JsonResult AstheOwner(@RequestBody Map<String, Object> params) {

        return wxbiz.AstheOwner(params);

    }

    /**
     * 查询投诉类型
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getComplaintType")
    public JsonResult getComplaintType() {
        return operationsFeignClient.getComplaintType();
    }

}
